package cn.itcast.web.controller.packing;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.util.DownloadUtils;
import cn.itcast.domain.packing.ShippingOrder;
import cn.itcast.service.packing.ShippingOrderService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;

    @RequestMapping(value = "toAdd", name = "去新增页面")
    public String toAdd(String id) {
        request.setAttribute("packingListId", id);
        return "packing/shipping/shipping-toShipping";
    }

    @RequestMapping(value = "list", name = "委托列表")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        PageInfo page = shippingOrderService.findByPage(pageNum, pageSize, null);
        request.setAttribute("page", page);
        return "packing/shipping/shipping-list";
    }

    @RequestMapping(value = "/edit", name = "新增/修改")
    public String edit(ShippingOrder shipping, String packingListId) {

        if (StrUtil.hasEmpty(shipping.getShippingOrderId())) {
            shipping.setShippingOrderId(packingListId);
            shippingOrderService.save(shipping);
        } else {
            shippingOrderService.update(shipping);
        }
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "去修改页面")
    public String toUpdate(String shippingOrderId) {
        ShippingOrder shipping = shippingOrderService.findById(shippingOrderId);
        request.setAttribute("shipping", shipping);
        return "packing/shipping/shipping-update";
    }

    @RequestMapping(value = "/delete", name = "删除委托")
    public String delete(String shippingOrderId) {
        shippingOrderService.delete(shippingOrderId);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/submit", name = "委托上报")
    public String submit(String shippingOrderId) {

        ShippingOrder shippingOrder = shippingOrderService.findById(shippingOrderId);
        shippingOrder.setState(1);
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消上报委托")
    public String cancel(String shippingOrderId) {
        ShippingOrder shippingOrder = shippingOrderService.findById(shippingOrderId);
        shippingOrder.setState(0);
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/printExcel", name = "去下载excel")
    public void printExcel(String shippingOrderId) throws IOException {
        ShippingOrder shippingOrder = shippingOrderService.findById(shippingOrderId);
        Date createTime = shippingOrder.getCreateTime();
      /*  Date limitDate = shippingOrder.getLimitDate();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String createTimeStr = dateFormat.format(createTime);
        String limit = dateFormat.format(limitDate);*/

        String realPath = session.getServletContext().getRealPath("/make/selfxls/poi_shipping.xlsx");
        TemplateExportParams templateExportParams = new TemplateExportParams(realPath);

        Map<String, Object> map = BeanUtil.beanToMap(shippingOrder, false, true);
        map.put("title","自己Put一个标题");
       /* map.put("createTime",createTimeStr);
        map.put("limitDate",limit);*/
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, map);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "ship.xlsx");
    }
    @RequestMapping(value = "/printPdf", name = "去下载pdf")
    public void printPdf(String shippingOrderId) throws Exception{

        //获取真实路径
        String realPath = session.getServletContext().getRealPath("/jasper/self/ship.jasper");
        //填充数据
        ShippingOrder shippingOrder = shippingOrderService.findById(shippingOrderId);
//        ArrayList<ShippingOrder> list = new ArrayList<>();
//        list.add(shippingOrder);
        Date date = shippingOrder.getLoadingDate();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String loadingDate = dateFormat.format(date);
        Map<String, Object> map= BeanUtil.beanToMap(shippingOrder, false, true);
      //  Map<String, Object> map = new HashMap<>();
        map.put("title","既睡不醒,i也睡不着");

        map.put("loadingDate",loadingDate);
        //  JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);

       // JasperPrint jasperPrint = JasperFillManager.fillReport(realPath,map,jrDataSource);
        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JREmptyDataSource());
        //  3.生成PDF输出到客户端
       // JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

       JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream); //将pdf输出到字节数组流

        DownloadUtils.download(outputStream,response,"委托单.pdf");

    }
}
